#include "BankSystem.hh"
#include <iostream>
#include <limits>
#include <iomanip>
void clearScreen() {
    #ifdef _WIN32
        system("cls");
    #else
        system("clear");
    #endif
}

void waitForEnter() {
    std::cout << "\nPress Enter to continue...";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

void displayMainMenu() {
    clearScreen();
    std::cout << "===== Banking System =====" << std::endl
              << "1. Register new customer" << std::endl
              << "2. Access customer accounts" << std::endl
              << "3. Display all bank accounts" << std::endl
              << "4. Transaction reports" << std::endl
              << "5. Exit" << std::endl
              << "Please choose: ";
}

void handleNewCustomer(BankSystem& bank) {
    clearScreen();
    std::cout << "===== Register New Customer =====" << std::endl;
    std::cout << "Enter customer name: ";
    std::string name;
    std::cin.ignore();
    std::getline(std::cin, name);

    if (bank.registerCustomer(name)) {
        auto accounts = bank.getCustomerAccounts(name);
        std::cout << "\nCustomer " << name << " successfully registered!" << std::endl
                 << "Two bank accounts have been created:" << std::endl
                 << "- Savings Account: " << accounts.first->getAccountId() << std::endl
                 << "- Checking Account: " << accounts.second->getAccountId() << std::endl;
    } else {
        std::cout << "Error: Customer already exists!" << std::endl;
    }
    waitForEnter();
}

void handleAccountAccess(const std::shared_ptr<IAccount>& account) {
    while (true) {
        clearScreen();
        std::cout << "===== Bank Account: " << account->getAccountId() << " =====" << std::endl
                 << "Account holder: " << account->getOwnerName() << std::endl
                 << "Balance: ¥" << std::fixed << std::setprecision(2) << account->getBalance() << std::endl
                 << std::endl;

        auto checkingAcc = dynamic_cast<CheckingAccount*>(account.get());
        if (checkingAcc) {
            std::cout << "1. Deposit" << std::endl
                     << "2. Withdraw" << std::endl
                     << "3. Transfer" << std::endl
                     << "4. View transaction history" << std::endl
                     << "5. Generate bank account report" << std::endl
                     << "6. Return to customer page" << std::endl;
        } else {
            std::cout << "1. Transfer" << std::endl
                     << "2. View transaction history" << std::endl
                     << "3. Generate bank account report" << std::endl
                     << "4. Return to customer page" << std::endl;
        }

        std::cout << "\nPlease choose: ";
        int choice;
        std::cin >> choice;

        // Handle menu options...
        if (choice == 6 || (!checkingAcc && choice == 4)) break;
    }
}

int main() {
    BankSystem bank;
    
    while (true) {
        displayMainMenu();
        
        int choice;
        std::cin >> choice;
        
        switch (choice) {
            case 1:
                handleNewCustomer(bank);
                break;
            case 2:
                // Handle customer access
                break;
            case 3:
                // Display all accounts
                break;
            case 4:
                // Generate transaction report
                break;
            case 5:
                std::cout << "Thank you for using our banking system. Goodbye!" << std::endl;
                return 0;
            default:
                std::cout << "Invalid choice. Please try again." << std::endl;
                waitForEnter();
        }
    }
    
    return 0;
}